<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="关联订单id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="关联订单id"></el-input>
    </el-form-item>
    <el-form-item label="任务类型，1为取件任务，2为派件任务" prop="taskType">
      <el-input v-model="dataForm.taskType" placeholder="任务类型，1为取件任务，2为派件任务"></el-input>
    </el-form-item>
    <el-form-item label="任务状态，1为待执行（对应 待上门和须交接）、2为进行中（该状态暂不使用，属于保留状态）、3为待确认（对应 待妥投和须交件）、4为已完成、5为已取消" prop="status">
      <el-input v-model="dataForm.status" placeholder="任务状态，1为待执行（对应 待上门和须交接）、2为进行中（该状态暂不使用，属于保留状态）、3为待确认（对应 待妥投和须交件）、4为已完成、5为已取消"></el-input>
    </el-form-item>
    <el-form-item label="签收状态(1为已签收，2为拒收)" prop="signStatus">
      <el-input v-model="dataForm.signStatus" placeholder="签收状态(1为已签收，2为拒收)"></el-input>
    </el-form-item>
    <el-form-item label="网点ID" prop="agencyId">
      <el-input v-model="dataForm.agencyId" placeholder="网点ID"></el-input>
    </el-form-item>
    <el-form-item label="快递员ID" prop="courierId">
      <el-input v-model="dataForm.courierId" placeholder="快递员ID"></el-input>
    </el-form-item>
    <el-form-item label="预计开始时间" prop="estimatedStartTime">
      <el-input v-model="dataForm.estimatedStartTime" placeholder="预计开始时间"></el-input>
    </el-form-item>
    <el-form-item label="实际开始时间" prop="actualStartTime">
      <el-input v-model="dataForm.actualStartTime" placeholder="实际开始时间"></el-input>
    </el-form-item>
    <el-form-item label="预计完成时间" prop="estimatedEndTime">
      <el-input v-model="dataForm.estimatedEndTime" placeholder="预计完成时间"></el-input>
    </el-form-item>
    <el-form-item label="实际完成时间" prop="actualEndTime">
      <el-input v-model="dataForm.actualEndTime" placeholder="实际完成时间"></el-input>
    </el-form-item>
    <el-form-item label="确认时间" prop="confirmTime">
      <el-input v-model="dataForm.confirmTime" placeholder="确认时间"></el-input>
    </el-form-item>
    <el-form-item label="取消时间" prop="cancelTime">
      <el-input v-model="dataForm.cancelTime" placeholder="取消时间"></el-input>
    </el-form-item>
    <el-form-item label="任务分配状态(1未分配2已分配3待人工分配)" prop="assignedStatus">
      <el-input v-model="dataForm.assignedStatus" placeholder="任务分配状态(1未分配2已分配3待人工分配)"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="mark">
      <el-input v-model="dataForm.mark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="任务创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="任务创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderId: '',
          taskType: '',
          status: '',
          signStatus: '',
          agencyId: '',
          courierId: '',
          estimatedStartTime: '',
          actualStartTime: '',
          estimatedEndTime: '',
          actualEndTime: '',
          confirmTime: '',
          cancelTime: '',
          assignedStatus: '',
          mark: '',
          createTime: ''
        },
        dataRule: {
          orderId: [
            { required: true, message: '关联订单id不能为空', trigger: 'blur' }
          ],
          taskType: [
            { required: true, message: '任务类型，1为取件任务，2为派件任务不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '任务状态，1为待执行（对应 待上门和须交接）、2为进行中（该状态暂不使用，属于保留状态）、3为待确认（对应 待妥投和须交件）、4为已完成、5为已取消不能为空', trigger: 'blur' }
          ],
          signStatus: [
            { required: true, message: '签收状态(1为已签收，2为拒收)不能为空', trigger: 'blur' }
          ],
          agencyId: [
            { required: true, message: '网点ID不能为空', trigger: 'blur' }
          ],
          courierId: [
            { required: true, message: '快递员ID不能为空', trigger: 'blur' }
          ],
          estimatedStartTime: [
            { required: true, message: '预计开始时间不能为空', trigger: 'blur' }
          ],
          actualStartTime: [
            { required: true, message: '实际开始时间不能为空', trigger: 'blur' }
          ],
          estimatedEndTime: [
            { required: true, message: '预计完成时间不能为空', trigger: 'blur' }
          ],
          actualEndTime: [
            { required: true, message: '实际完成时间不能为空', trigger: 'blur' }
          ],
          confirmTime: [
            { required: true, message: '确认时间不能为空', trigger: 'blur' }
          ],
          cancelTime: [
            { required: true, message: '取消时间不能为空', trigger: 'blur' }
          ],
          assignedStatus: [
            { required: true, message: '任务分配状态(1未分配2已分配3待人工分配)不能为空', trigger: 'blur' }
          ],
          mark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '任务创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/aggregation/taskpickupdispatch/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderId = data.taskPickupDispatch.orderId
                this.dataForm.taskType = data.taskPickupDispatch.taskType
                this.dataForm.status = data.taskPickupDispatch.status
                this.dataForm.signStatus = data.taskPickupDispatch.signStatus
                this.dataForm.agencyId = data.taskPickupDispatch.agencyId
                this.dataForm.courierId = data.taskPickupDispatch.courierId
                this.dataForm.estimatedStartTime = data.taskPickupDispatch.estimatedStartTime
                this.dataForm.actualStartTime = data.taskPickupDispatch.actualStartTime
                this.dataForm.estimatedEndTime = data.taskPickupDispatch.estimatedEndTime
                this.dataForm.actualEndTime = data.taskPickupDispatch.actualEndTime
                this.dataForm.confirmTime = data.taskPickupDispatch.confirmTime
                this.dataForm.cancelTime = data.taskPickupDispatch.cancelTime
                this.dataForm.assignedStatus = data.taskPickupDispatch.assignedStatus
                this.dataForm.mark = data.taskPickupDispatch.mark
                this.dataForm.createTime = data.taskPickupDispatch.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/aggregation/taskpickupdispatch/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderId': this.dataForm.orderId,
                'taskType': this.dataForm.taskType,
                'status': this.dataForm.status,
                'signStatus': this.dataForm.signStatus,
                'agencyId': this.dataForm.agencyId,
                'courierId': this.dataForm.courierId,
                'estimatedStartTime': this.dataForm.estimatedStartTime,
                'actualStartTime': this.dataForm.actualStartTime,
                'estimatedEndTime': this.dataForm.estimatedEndTime,
                'actualEndTime': this.dataForm.actualEndTime,
                'confirmTime': this.dataForm.confirmTime,
                'cancelTime': this.dataForm.cancelTime,
                'assignedStatus': this.dataForm.assignedStatus,
                'mark': this.dataForm.mark,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
